<template>
  <div ref="wrapper">
    <div>
      <slot></slot>
    </div>
  </div>
</template>

<script>

import BScroll from 'better-scroll'
import observeDom from '@better-scroll/observe-dom'
import observeImage from '@better-scroll/observe-image'
//
//
BScroll.use(observeDom)
BScroll.use(observeImage)


export default {
  name: "Scroll",
  data() {
    return {
      bs: null
    }
  },
  props: {
    // 组件需要传入参数以决定是否监听滚动
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  }
  ,
  mounted() {
    this.bs = new BScroll(this.$refs.wrapper, {
      // 监听dom和图片的刷新
      observeDom: true,
      observeImage: true,
      click: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad
    })

    this.bs.on('scroll', (position) => {
      this.$emit('scroll', position)
    })
    if(this.pullUpLoad){
      this.bs.on('pullingUp', () => {
        this.$emit('pullingUp')
      })
    }

  },
  methods: {
    scrollTo(x, y, time = 300) {
      this.bs && this.bs.scrollTo(x, y, time)
    },
    finishPullUp() {
      this.bs && this.bs.finishPullUp()
    },
    refresh() {
      this.bs && this.bs.refresh()
    },
    getScrollY() {
      return this.bs.y ? this.bs.y : 0
    }


  }
}
</script>

<style scoped>
</style>
